<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="项目编号" prop="project_id">
                        <el-input v-model="project_code" readonly placeholder="请选择项目" @click="checkProject"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="项目名称" prop="project_name">
                        <el-input v-model="project_name" readonly placeholder="项目名称"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="销售" prop="salesman">
                        <el-select v-model="form.salesman" filterable clearable placeholder="请选择销售" style="width:100%">
                            <el-option v-for="item in saletList" :key="item.id" :label="item.name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="附件" prop="contract_files">
                        <sc-upload-file v-model="form.contract_files">
                            <el-button type="primary" size="small">上传文件</el-button>
                        </sc-upload-file>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" clearable placeholder="备注" type="textarea"></el-input>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <save-project v-if="dialog.project" ref="projectDialog" @success="projectBack" @closed="dialog.project = false" draggable></save-project>
</template>

<script>
import saveProject from '@/views/project/list/itemList.vue'
export default {
    components: { saveProject },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增合同',
				edit: '编辑合同',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
                contract_code:"",
                project_id:"",
                salesman:"",
                contract_files:"",
                remark:"",
			},
            project_code:"",
            project_name:"",
			//验证规则
			rules: {
				enterprise: [{ required: true, message: '请输入名称' }],
			},
            saletList:[],
            dialog:{
                project:false,
            },
		}
	},
	mounted() {
        this.getUser()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.project.contract.edit.post(this.form);
					} else {
						res = await this.$API.project.contract.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
        projectData(data){
            this.form.project_id = data.project_id
            this.project_code = data.project_code
            this.project_name = data.referrer_name
        },
		//表单注入数据
		setData(data) {
            this.form.id = data.id
            this.form.project_id = data.project_id
            this.form.contract_code = data.contract_code
            this.form.contract_files = data.contract_files
            this.form.salesman = Number(data.salesman)
            this.form.remark = data.remark
            this.project_code = data.project.project_code
            this.project_name = data.project.metting
		},
        onFocus(event){
            event.target.select();
        },
        // 销售
        async getUser(){
            var res = await this.$API.system.admin.all.get();
            this.saletList = res.data;
        },
        checkProject(){
            this.dialog.project = true
			this.$nextTick(() => {
				this.$refs.projectDialog.open('add')
			})
        },
        projectBack(data){
            this.form.project_id = data.id
            this.project_code = data.project_code
            this.project_name = data.metting
        },
	}
}
</script>

<style></style>
